﻿using System;
using System.Collections.Generic;
using System.Linq;
using ComicQ.Domain.Table;
using NHibernate.Linq;

namespace ComicQ.Controller
{
    public class AuthorController
    {
        public IList<Author> GetAllAuthor()
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                return session.Query<Author>().ToList();
            }
        }

        public IList<String> GetAllAuthorName()
        {
            return GetAllAuthor().Select(author => author.Name).ToList();
        }

        public string GetAuthorName(Author author)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                return session.Query<Author>().ToList().
                    Where(a => a.Id == author.Id).
                    Select(a => a.Name).
                    Single();
            }
        }

        public int AddAuthor(Author author)
        {
            try
            {
                GetAllAuthor()
                    .Where(a => a.Name == author.Name)
                    .Select(a => a)
                    .Single();

                return 0;
            }
            catch (Exception)
            {
                using (var session = NHibernateHelper.OpenSession())
                {
                    using (var transaction = session.BeginTransaction())
                    {
                        session.Save(author);
                        transaction.Commit();
                    }
                }

                return 1;
            }
        }

        public int EditAuthor(Author newAuthor, String oldName)
        {
            try
            {
                var checkAuthor = GetAllAuthor()
                    .Where(a => a.Name == newAuthor.Name)
                    .Select(a => a)
                    .Single();

                if (checkAuthor.Name == oldName)
                {
                    using (var session = NHibernateHelper.OpenSession())
                    {
                        using (var transaction = session.BeginTransaction())
                        {
                            session.SaveOrUpdate(newAuthor);
                            transaction.Commit();
                        }
                    }

                    return 1;
                }

                return 0;
            }
            catch (Exception)
            {
                using (var session = NHibernateHelper.OpenSession())
                {
                    using (var transaction = session.BeginTransaction())
                    {
                        session.SaveOrUpdate(newAuthor);
                        transaction.Commit();
                    }
                }

                return 1;
            }
        }

        public void DeleteAuthor(Author author)
        {
            using (var session = NHibernateHelper.OpenSession())
            {
                using (var transaction = session.BeginTransaction())
                {
                    session.Delete(author);
                    transaction.Commit();
                }
            }
        }
    }
}
